package test.patches;

import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.cards.CardQueueItem;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.orbs.AbstractOrb;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.unlock.UnlockTracker;
import test.character.Captain;
import test.monitors.SeaMonitor;

//让手牌都沉在左侧
public class RefreshHandLayoutPatch {

    @SpirePatch(
            clz = CardGroup.class,
            method = "refreshHandLayout"
    )
    public static class AnglePatch {
        @SpirePrefixPatch
        public static SpireReturn prefix(CardGroup __instance) {
            if(!(AbstractDungeon.player instanceof Captain)){
                return SpireReturn.Continue();
            }

            if (AbstractDungeon.getCurrRoom().monsters == null || !AbstractDungeon.getCurrRoom().monsters.areMonstersBasicallyDead()) {
                if (AbstractDungeon.player.hasPower("Surrounded") && AbstractDungeon.getCurrRoom().monsters != null) {
                    for (AbstractMonster m : AbstractDungeon.getCurrRoom().monsters.monsters) {
                        if (AbstractDungeon.player.flipHorizontal) {
                            if (AbstractDungeon.player.drawX < m.drawX) {
                                m.applyPowers();
                            } else {
                                m.applyPowers();
                                m.removeSurroundedPower();
                            }
                        } else if (!AbstractDungeon.player.flipHorizontal) {
                            if (AbstractDungeon.player.drawX > m.drawX) {
                                m.applyPowers();
                            } else {
                                m.applyPowers();
                                m.removeSurroundedPower();
                            }
                        }
                    }
                }

                for (AbstractOrb o : AbstractDungeon.player.orbs) {
                    o.hideEvokeValues();
                }

                if (AbstractDungeon.player.hand.size() + AbstractDungeon.player.drawPile.size() + AbstractDungeon.player.discardPile.size() <= 3 && AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT && AbstractDungeon.getCurrRoom().monsters != null && !AbstractDungeon.getCurrRoom().monsters.areMonstersBasicallyDead() && AbstractDungeon.floorNum > 3) {
                    UnlockTracker.unlockAchievement("PURITY");
                }

                for (AbstractRelic r : AbstractDungeon.player.relics) {
                    r.onRefreshHand();
                }

                for (int i = 0; i < __instance.group.size(); ++i) {
                    if(i<SeaMonitor.MAX_HAND_SIZE){
                        __instance.group.get(i).setAngle(0);
                        __instance.group.get(i).target_y = SeaMonitor.Y_OFFSET * Settings.scale;
                    }
                }

                for (AbstractCard c : __instance.group) {
                    c.targetDrawScale = SeaMonitor.CARD_SCALA;
                }

                for (int i = 0; i < __instance.group.size(); i++) {
                    if(i<SeaMonitor.MAX_HAND_SIZE){
                        SeaMonitor.Sea sea= SeaMonitor.getSeaByCard(__instance.group.get(i));
                        if(sea!=null){
                            int index = sea.index;
                            if(index!=-1){
                                __instance.group.get(i).target_x = (float) Settings.WIDTH / 2.0F + AbstractCard.IMG_WIDTH_S * SeaMonitor.X_OFFSET.get(index);
                            }
                        }
                    }
                }

                for (AbstractCard c : __instance.group) {
                    //c.targetDrawScale = 0.63750005F;
                    c.targetDrawScale = SeaMonitor.CARD_SCALA;
                }

                AbstractCard card = AbstractDungeon.player.hoveredCard;
                if (card != null) {
                    card.setAngle(0.0F);
                    card.target_x = (card.current_x + card.target_x) / 2.0F;
                    card.target_y = card.current_y;
                }

                for (CardQueueItem q : AbstractDungeon.actionManager.cardQueue) {
                    if (q.card != null) {
                        q.card.setAngle(0.0F);
                        q.card.target_x = q.card.current_x;
                        q.card.target_y = q.card.current_y;
                    }
                }

                __instance.glowCheck();
            }
            return SpireReturn.Return();
        }

    }
}
